package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.DeviceTransferRecordMapper;
import com.zhijian.medical.entity.po.DeviceTransferRecord;
import com.zhijian.medical.entity.vo.request.DeviceTransferRecordSearchVo;
import com.zhijian.medical.entity.vo.response.DeviceTransferRecordVo;
import com.zhijian.medical.service.DeviceTransferRecordService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 仪器调动记录业务处理层
 *
 * @author HCG
 * @date 2022-01-01 15:40:34
 * @version: 1.0
 */
@Slf4j
@Service
public class DeviceTransferRecordServiceImpl implements DeviceTransferRecordService {

    @Autowired
    private DeviceTransferRecordMapper mapper;

    @Override
    public DeviceTransferRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceTransferRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DeviceTransferRecord deviceTransferRecord) {
        Date now = new Date();
        deviceTransferRecord.setCreateTime(now);
        deviceTransferRecord.setUpdateTime(now);
        return mapper.insert(deviceTransferRecord) == 1 ? deviceTransferRecord.getId() : 0L;
    }

    @Override
    public boolean update(DeviceTransferRecord deviceTransferRecord) {
        Date now = new Date();
        deviceTransferRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceTransferRecord) != 0;
    }

    @Override
    public Integer count(DeviceTransferRecordSearchVo deviceTransferRecordSearchVo) {
        return mapper.count(deviceTransferRecordSearchVo);
    }

    @Override
    public List<DeviceTransferRecordVo> selectList(DeviceTransferRecordSearchVo deviceTransferRecordSearchVo) {
        return mapper.selectList(deviceTransferRecordSearchVo);
    }

    @Override
    public Page<DeviceTransferRecordVo> selectPage(DeviceTransferRecordSearchVo deviceTransferRecordSearchVo) {
        //先直接查总数
        int count = count(deviceTransferRecordSearchVo);
        //分页
        Page<DeviceTransferRecordVo> page = new Page<>(true, count, deviceTransferRecordSearchVo.getPageNum(), deviceTransferRecordSearchVo.getPageSize());
        deviceTransferRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceTransferRecordVo> responseVos = this.selectList(deviceTransferRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

}